package algorithm;

import javax.swing.*;
import java.awt.image.Kernel;
import java.util.Arrays;

//克鲁斯卡尔算法
//最小生成树
public class Kruskal {
    private int edgeNum;
    private char[] vertexs;
    private int[][] matrix;
    private static final int INF = Integer.MAX_VALUE;

    public static void main(String[] args) {
        char[] vertexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int matrix[][] = {
                {0, 12, INF, INF, INF, 16, 14},
                {12, 0, 10, INF, INF, 7, INF},
                {INF, 10, 0, 3, 5, 6, INF},
                {INF, INF, 3, 0, 4, INF, INF},
                {INF, INF, 5, 4, 0, 2, 8},
                {16, 7, 6, INF, 2, 0, 9},
                {14, INF, INF, INF, 8, 9, 0}

        };
        Kruskal kruskal = new Kruskal(vertexs, matrix);
//        kruskal.print();
        EDate[] edges = kruskal.getEdges();
//        System.out.println(Arrays.toString(edges));
        kruskal.sortEdges(edges);
//        System.out.println("排序后-----------------");
//        System.out.println(Arrays.toString(edges));
        kruskal.kruskal();
    }

    private void print() {
        System.out.println("邻接矩阵为:");
        for (int i = 0; i < vertexs.length; i++) {
            for (int j = 0; j < vertexs.length; j++) {
                System.out.printf("%12d", matrix[i][j]);
            }
            System.out.println();
        }
    }

    //克鲁斯卡尔算法求最小生成树
    public void kruskal() {
        int index = 0;
        int[] ends = new int[edgeNum];
        EDate[] rets = new EDate[edgeNum];
        EDate[] edges = getEdges();
        sortEdges(edges);

        //遍历
        for (int i = 0; i < edgeNum; i++) {
            int p1 = getPosotoon(edges[i].start);
            int p2 = getPosotoon(edges[i].end);
            //获取p1，p2这  两个顶点在已有最小生成树中的终点
            int m = getEnd(ends, p1);
            int n = getEnd(ends, p2);
            if (m != n) {
                ends[m] = n;
                rets[index++]=edges[i];
            }
        }
        System.out.println("最小生成树为:"+Arrays.toString(rets));
    }

    private Kruskal(char[] vertexs, int[][] matrix) {
        int vlen = vertexs.length;
        this.vertexs = new char[vlen];
        this.vertexs = vertexs;
        this.matrix = new int[vlen][vlen];
        this.matrix = matrix;

        for (int i = 0; i < vlen; i++) {
            for (int j = i + 1; j < vlen; j++) {
                if (this.matrix[i][j] != INF) {
                    edgeNum++;
                }
            }
        }
    }

    //对边进行排序
    private void sortEdges(EDate[] edges) {
        for (int i = 0; i < edges.length - 1; i++) {
            for (int j = 0; j < edges.length - 1 - i; j++) {
                if (edges[j].weight > edges[j + 1].weight) {
                    EDate tmp = edges[j];
                    edges[j] = edges[j + 1];
                    edges[j + 1] = tmp;
                }
            }
        }
    }

    //返回定点的下标，未找到则返回-1
    public int getPosotoon(char ch) {
        for (int i = 0; i < vertexs.length; i++) {
            if (vertexs[i] == ch) {
                return i;
            }
        }
        return -1;
    }

    //获取图中的边，当到EData[]数组中，后面我们需要遍历数组
    private EDate[] getEdges() {
        int index = 0;
        EDate[] edges = new EDate[edgeNum];
        for (int i = 0; i < vertexs.length; i++) {
            for (int j = i + 1; j < vertexs.length; j++) {
                if (matrix[i][j] != INF) {
                    edges[index++] = new EDate(vertexs[i], vertexs[j], matrix[i][j]);
                }
            }
        }
        return edges;
    }

    //获取下标为i的顶点的终点

    /**
     * @param ends 数组就是记录了各个顶点对应的终点是哪个，ends是在遍历过程中形成的
     * @param i
     * @return
     */
    private int getEnd(int[] ends, int i) {
        while (ends[i] != 0) {
            i = ends[i];
        }
        return i;
    }
}

//表示一条变
class EDate {
    char start;
    char end;
    int weight;

    public EDate(char start, char end, int weight) {
        this.start = start;
        this.end = end;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "EDate{" +
                "start=" + start +
                ", end=" + end +
                ", weight=" + weight +
                '}';
    }
}